﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class ManageUserRoles : SOSecurityPage
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateUserCredentials(Request.Url.LocalPath);
            SetDataSourceToTypes(structureFacade.GetUserTypes(""));
        }

        protected void btnCreateRole_Click(object sender, EventArgs e)
        {
            if (ddlRoleType.SelectedIndex > 0)
            {
                if (!Roles.RoleExists(txtRole.Text))
                {
                    Roles.CreateRole(txtRole.Text);
                }

                /*Get Role by name*/
                UserRole_aspnet userRole_aspnet = structureFacade.GetAspnetUserRole(txtRole.Text);

                /*Get ID of the record*/
                Guid roleID = userRole_aspnet.RoleId;

                /*Get Other Records */
                short userTypeID = Convert.ToInt16(ddlRoleType.SelectedValue);
                /*Insert To DataBase*/
                UserRole userRole = new UserRole();
                userRole.RoleId = roleID;
                userRole.UserTypeId = userTypeID;
                userRole.Status = 1;
                structureFacade.InsertUserRole(userRole);

                Response.Redirect("~/UserManagement/AssignPermissions.aspx");
            }

        }

        protected void ddlRoleType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void SetDataSourceToTypes(List<UserType> userTypes)
        {
            ddlRoleType.DataSource = userTypes;
            ddlRoleType.DataTextField = "UserTypeName";
            ddlRoleType.DataValueField = "UserTypeId";
            ddlRoleType.DataBind();


            //InsertDDLItems(ddlRoleType, "Select...", 0);
            //InsertDDLItems(ddlFrom, "Select All", 1);
        }

        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            ddl.Items.Insert(Position, Text);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/ViewRole.aspx");
        }
    }
}
